/**
 * Booking List
 */
Divo.app.BookingList = function() {

	/* ----------------------- private属性 ----------------------- */
	var queryUrl = Divo.getPath() + 'servlet/Sample?action=bookingList';
	var gridEl = 'booking-grid';
	var pageSize = 10;

	var grid, ds, selModel, tb;
	var cId, cIdFocus;
	var bookCandelBtn;

	/* ----------------------- private方法 ----------------------- */
	// 创建网格
	function createGrid() {
		createToolbarBtn();
		
		var recordDef =  Ext.data.Record.create([
			{name : "id", type : "int"},
			{name : "name", type : "string"},
			{name : "address", type : "string"},
			{name : "nights", type : "int"},
			{name : "room", type : "string"}
		]);

		var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
			header : "Name",
			width : 120,
			sortable : true,
			dataIndex : 'name'
		}, {
			header : "Address",
			width : 120,
			sortable : true,
			dataIndex : 'address'
		}, {
			header : "Nights",
			width : 50,
			sortable : true,
			dataIndex : 'nights'
		}, {
			header : "Room Preference",
			width : 200,
			sortable : true,
			dataIndex : 'room'
		}
		]);
		
		ds = new Ext.data.Store( {
				proxy : new Ext.data.HttpProxy( {
					url : queryUrl,
					method: 'GET'    //没有这个配置url就无效
				}),
				reader : new Ext.data.JsonReader( {
					root : 'rows',
					totalProperty : 'totalCount',
					id : 'id'
				}, recordDef),
				remoteSort : true
			});

		var config = {
			el : gridEl,
			region : 'center',
			store : ds,
			sm : new Ext.grid.RowSelectionModel( {
				singleSelect : true
			}),
			cm : cm,
			loadMask : Divo.loadMask,
			tbar: tb,
			bbar : new Ext.PagingToolbar( {
				pageSize : pageSize,
				store : ds,
				displayInfo : true
			})
		};
		
   		grid = new Ext.grid.GridPanel(config);
	}

	// 选择某行
	function onRowSelect(grid, rowIndex, e) {
		if (cIdFocus)
			cId = cIdFocus;
		else
			cId = selModel.getSelections()[0].data.id;
		cIdFocus = null;
		setToolbarStatus();
	}

	// 加载数据完成后
	function onLoad(store, records, options) {
		if (ds.getTotalCount() === 0) {
			cId = null;
		} else {
			if (cId)
				selModel.selectRow(ds.indexOfId(cId));
			else
				selModel.selectFirstRow();
		}
		setToolbarStatus();
	}

	// 添加工具条按钮
	function createToolbarBtn() {
		tb = [];
		bookCandelBtn = new Ext.Toolbar.Button( {
			text : 'Cancel',
			tooltip : 'Delete Booked Hotel',
			icon : Divo.getIconDelete(),
			cls : 'x-btn-text-icon',
			handler : bookHotelCancel
		});
		tb.push(bookCandelBtn);
	}

	// 设置网格工具条按钮状态
	function setToolbarStatus() {
		if (ds.getTotalCount() === 0) {
			bookCandelBtn.disable();
		} else {
			bookCandelBtn.enable();
		}
	}

	// 撤销预订
	function bookHotelCancel() {
		if (!selModel.hasSelection()) {
			Divo.say('请先选择某个酒店。');
			return;
		}
		Ext.MessageBox.confirm('确认', '要撤销预订吗？', function(btn) {
			if (btn !== 'yes')
				return;

			var onDeleted = function() {
				Divo.say("撤销成功");
				ds.reload();
			}

			Divo.wait("正在撤销...");
			Divo.rmtCallAsync("sampleFacade.deleteBooking",onDeleted,[cId]);
		});
	}

	//预订保存后
	function onNewBooking(subj, msg, data) {
		cIdFocus = msg.id;
		ds.reload();
	}
	
	/* ----------------------- public方法 ----------------------- */
	return {
		/**
		 * 初始化
		 */
		init : function() {
			//订阅消息
			Divo.subscribe(Divo.msg.BOOKING_ADD,onNewBooking,this);
			
			createGrid();
		},
		/**
		 * 返回Ext网格对象
		 */
		getGrid : function() {
			return grid;
		},
		/**
		 * 展现网格
		 */
		showGrid : function() {
			grid.render();
			selModel = grid.getSelectionModel();

			selModel.on('rowselect', onRowSelect);
			ds.on("load", onLoad);

			ds.load( {
				params : {
					start : 0,
					limit : pageSize
				}
			});
		}

	}; // return

}();

// EOP

